package cn.androidservice;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {
    private MyBinder myBind;
    private boolean isService = true;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("MyService", "onCreate");

        myBind = new MyBinder();

//        获取当前时间的毫秒数
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isService) {
                    Log.e("MyService", "time=" + System.currentTimeMillis());

                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return myBind;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("MyService", "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        isService = false;
        Log.e("MyService", "onDestroy");
        super.onDestroy();
    }

}
